package com.fancy.music.controller.admin;

import com.fancy.common.dto.PageResult;
import com.fancy.common.dto.response.Result;
import com.fancy.music.dto.AlbumDetailDTO;
import com.fancy.music.entity.Album;
import com.fancy.music.service.AlbumService;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "管理员-专辑管理")
@RestController
@RequestMapping("/api/v1/admin/albums")
@RequiredArgsConstructor
public class AdminAlbumController {

    private final AlbumService albumService;

    @ApiOperation(value = "管理员获取专辑列表", notes = "管理员分页获取专辑列表，支持多条件筛选")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "albumName", value = "专辑名称", paramType = "query", dataType = "string"),
        @ApiImplicitParam(name = "artistName", value = "歌手名称", paramType = "query", dataType = "string"),
        @ApiImplicitParam(name = "genre", value = "音乐风格", paramType = "query", dataType = "string"),
        @ApiImplicitParam(name = "status", value = "状态(0-禁用,1-正常)", paramType = "query", dataType = "int"),
        @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "int", example = "1"),
        @ApiImplicitParam(name = "size", value = "每页大小", paramType = "query", dataType = "int", example = "10")
    })
    @GetMapping
    public Result<PageResult<AlbumDetailDTO>> getAlbumManageList(
            @RequestParam(required = false) String albumName,
            @RequestParam(required = false) String artistName,
            @RequestParam(required = false) String genre,
            @RequestParam(required = false) Integer status,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {

        // 构建搜索关键词，优先使用专辑名称，如果为空则使用歌手名称
        String keyword = null;
        if (albumName != null && !albumName.trim().isEmpty()) {
            keyword = albumName.trim();
        } else if (artistName != null && !artistName.trim().isEmpty()) {
            keyword = artistName.trim();
        }

        PageResult<AlbumDetailDTO> result = albumService.searchAlbumsForAdmin(keyword, genre, status, page, size);
        return Result.success(result);
    }

    @ApiOperation(value = "创建�����", notes = "管理员添加新专辑")
    @ApiParam(name = "album", value = "专辑信息", required = true)
    @PostMapping
    public Result<Long> createAlbum(@RequestBody Album album) {
        Long albumId = albumService.createAlbum(album);
        if (albumId != null) {
            return Result.success(albumId);
        } else {
            return Result.fail("创建专辑失败");
        }
    }

    @ApiOperation(value = "更新专辑信息", notes = "管理员修改专辑信息")
    @ApiImplicitParam(name = "albumId", value = "专辑ID", required = true, paramType = "path", dataType = "long")
    @ApiParam(name = "album", value = "专辑信息", required = true)
    @PutMapping("/{albumId}")
    public Result<Void> updateAlbum(@PathVariable Long albumId, @RequestBody Album album) {
        album.setAlbumId(albumId);
        boolean success = albumService.updateAlbum(album);
        if (success) {
            return Result.success(null);
        } else {
            return Result.fail("更新专辑失败");
        }
    }

    @ApiOperation(value = "删除专辑", notes = "管理员删除指定专辑")
    @ApiImplicitParam(name = "albumId", value = "专辑ID", required = true, paramType = "path", dataType = "long")
    @DeleteMapping("/{albumId}")
    public Result<Void> deleteAlbum(@PathVariable Long albumId) {
        boolean success = albumService.deleteAlbum(albumId);
        if (success) {
            return Result.success(null);
        } else {
            return Result.fail("删除专辑失败");
        }
    }

    @ApiOperation(value = "批量删除专辑", notes = "管理员批量删除多个专辑")
    @ApiParam(name = "albumIds", value = "专辑ID列表", required = true)
    @DeleteMapping("/batch")
    public Result<Void> batchDeleteAlbums(@RequestBody List<Long> albumIds) {
        int successCount = 0;
        for (Long albumId : albumIds) {
            if (albumService.deleteAlbum(albumId)) {
                successCount++;
            }
        }
        if (successCount > 0) {
            return Result.success(null);
        } else {
            return Result.fail("批量删除失败");
        }
    }

    @ApiOperation(value = "启用/禁用专辑", notes = "管理员修改专辑状态")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "albumId", value = "专辑ID", required = true, paramType = "path", dataType = "long"),
        @ApiImplicitParam(name = "status", value = "状态(0-禁用,1-启用)", required = true, paramType = "query", dataType = "int")
    })
    @PutMapping("/{albumId}/status")
    public Result<Void> toggleAlbumStatus(@PathVariable Long albumId, @RequestParam Integer status) {
        boolean success = albumService.toggleAlbumStatus(albumId, status);
        if (success) {
            return Result.success(null);
        } else {
            return Result.fail("修改专辑状态失败");
        }
    }
}
